package ezy.lite;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

public class PKGInfo {
    public String appName = "";
    public Drawable appIcon;
    public String packageName = "";
    public String versionName = "";
    public int versionCode;
    public long firstInstallTime;
    public long lastUpdateTime;

    public static PKGInfo get(Context context, String pkg) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(pkg, PackageManager.GET_CONFIGURATIONS);
            PKGInfo info = new PKGInfo();
            info.versionCode = pi.versionCode;
            info.versionName = pi.versionName;
            info.packageName = pi.packageName;
            info.firstInstallTime = pi.firstInstallTime;
            info.lastUpdateTime = pi.lastUpdateTime;

            info.appName = pm.getApplicationLabel(pi.applicationInfo).toString();
            info.appIcon = pm.getApplicationIcon(pkg);
            return info;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Drawable icon(Context context, String pkg) {
        try {
            return context.getPackageManager().getApplicationIcon(pkg);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}